package com.party.controller.sys.vote;

import com.party.common.Page;
import com.party.model.VotePerson;
import com.party.model.SysUser;
import com.party.service.excellentparty.VotePersonService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/votePerson")
public class VotePersonController {

    @Resource
    VotePersonService votePersonService;

    @Resource
    SysUserService sysUserService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/vote/votepersonlist");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String author = req.getParameter("author");
        String title = req.getParameter("title");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        VotePerson voteParty = new VotePerson();
        voteParty.setAuthor(author);
        voteParty.setTitle(title);
        Page p = this.votePersonService.getListPageByCustorm(voteParty, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount());
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 获取所有人
     * @return
     */
    @RequestMapping(value = "/allPersons")
    public ModelAndView voteParty(HttpServletRequest request) {
        String voteId=request.getParameter("voteid");
        ModelAndView modelAndView = new ModelAndView();
        List<SysUser> listUser=sysUserService.getAllUsers();

        List<VotePerson> selectListUser=votePersonService.getAllUsersByVoteId(Integer.parseInt(voteId));

        modelAndView.addObject("selectListUser",selectListUser);
        modelAndView.addObject("listUser",listUser);
        modelAndView.setViewName("/sys/vote/vote");
        modelAndView.addObject("voteid",voteId);
        return modelAndView;
    }

    /**
     * 添加投票人员
     * @return
     */
    @RequestMapping(value = "/editVotePerson")
    public ModelAndView editVotePerson(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String msg="";

        String voteid=request.getParameter("voteid");
        String userids[] = request.getParameterValues("userid");
        if(voteid==null||"".equals(voteid)){
            msg="添加成功";
        }else{
            Integer id=Integer.parseInt(voteid);
            int num=votePersonService.addVotePersonByUserIds(id,userids);
            if(num>0){
                msg="添加成功";
            }else{
                msg="添加失败";
            }
        }
        modelAndView.addObject("voteid",voteid);
        modelAndView.addObject("msg",msg);
        modelAndView.setViewName("/sys/vote/vote");
        return modelAndView;
    }

    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = votePersonService.deleteVotePersonByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
